package edu.usc.csci578.util;

import java.util.Vector;

import edu.usc.csci578.model.Cluster;
import edu.usc.csci578.model.SMethod;
import edu.usc.csci578.model.STopic;

public class ModelParser {
	public static boolean isSpec(String type) throws Exception {
		if (STopic.XML_TAG_SPEC.equals(type)) {
			return true;
		} else if (STopic.XML_TAG_INDEP.equals(type)) {
			return false;
		} else {
			throw new Exception("Special Topic Type!: " + type);
		}
	}

	public static boolean isClusterSpec(String type) throws Exception {
		if (Cluster.XML_TAG_SPEC.equals(type)) {
			return true;
		} else if (Cluster.XML_TAG_INDEP.equals(type)) {
			return false;
		} else {
			throw new Exception("Special Cluster Type!: " + type);
		}
	}

	/*
	 * public static Integer isDocTopicSpec(String type) throws Exception { if
	 * (DocTopic.XML_TAG_UNSPEC.equals(type)) { return DocTopic.TYPE_UNSPEC; }
	 * else if (DocTopic.XML_TAG_INDEP.equals(type)) { return
	 * DocTopic.TYPE_INDEP; } else if (DocTopic.XML_TAG_SPEC.equals(type)) {
	 * return DocTopic.TYPE_SPEC; } else { throw new
	 * Exception("Special DocTopic Type!: " + type); } }
	 */

	public static Integer getMethodType(String type) throws Exception {
		if (SMethod.XML_PUBLIC.equals(type)) {
			return SMethod.METHOD_TYPE_PUBLIC;
		} else if (SMethod.XML_PROTECTED.equals(type)) {
			return SMethod.METHOD_TYPE_PROTECTED;
		} else if (SMethod.XML_PRIVATE.equals(type)) {
			return SMethod.METHOD_TYPE_PRIVATE;
		} else {
			throw new Exception("Special Method Type!: " + type);
		}
	}

	public static String[] parseClassName(String name) {
		String[] names = new String[2];
		String temp = name.replaceAll("[()]", "");
		int i = temp.lastIndexOf(".");
		// System.out.println(temp);
		if (i > 0 && i < temp.length()) {
			names[1] = temp.substring(0, i);
			names[0] = temp.substring(i + 1);
		} else {
			names[0] = temp;
			names[1] = "";
		}
		// System.out.println(names[0] + " " + names[1]);
		return names;
	}

	public static String[] parseMethodName(String name) {
		String[] names = new String[3];
		String temp = name.replaceAll("[()]", "");
		String[] temps = temp.split(",");
		names[2] = temps[0];
		int i = temps[1].lastIndexOf(".");
		names[1] = temps[1].substring(0, i);
		names[0] = temps[1].substring(i + 1);
		return names;
	}

	public static boolean parseIsJave(String name) {
		if (name != null
				&& (name.startsWith("java") || name.startsWith("javex"))) {
			return true;
		}
		return false;
	}

	public static Vector<String> parseClusterName(String name) throws Exception {
		Vector<String> names = new Vector<String>();

		if ((!name.startsWith("(")) || (!name.endsWith(")")))
			throw new Exception(
					"Cluster name format error: not starts with '(' and ends with ')'");
		name = name.substring(1, name.length() - 1) + ',';

		int level = 0;
		int lastPos = 0;
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (c == '(')
				level++;
			else if (c == ')')
				level--;
			else if (c == ',') {
				if (level != 0)
					continue;
				names.add(name.substring(lastPos, i));
				lastPos = i + 1;
			}
		}

		// String[] ret = new String[names.size()];
		// for(int i=0;i<names.size();i++)
		// ret[i]=names.elementAt(i);
		return names;
	}

	public static void main(String args[]) throws Exception {
		test2();
	}

	public static void test1() {
		String test = "(public,org.apache.oodt.cas.filemgr.structs.type.ValueReplaceTypeHandler.&lt;init&gt;)";
		String[] name = parseMethodName(test);
		System.out.println(name[0] + " " + name[1] + " " + name[2]);
	}

	public static void test2() throws Exception {
		String test = "(((org.apache.oodt.cas.filemgr.catalog.DataSourceCatalogFactory),(org.apache.oodt.cas.filemgr.catalog.MappedDataSourceCatalogFactory)),(org.apache.oodt.cas.filemgr.catalog.LuceneCatalogFactory))";
		// String test = "(A,B)";
		Vector<String> names = parseClusterName(test);
		System.out.println("Test String = " + test);
		for (int i = 0; i < names.size(); i++)
			System.out.println("String " + i + " : " + names.elementAt(i));
	}
}